﻿using ServiceStack.CacheAccess;
using ServiceStack.CacheAccess.Providers;
using ServiceStack.ServiceInterface;
using ServiceStack.ServiceInterface.Auth;
using ServiceStack.WebHost.Endpoints;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using OpenThesis.Extensions.FX;

namespace OpenThesis.Rest
{
	//Define the Web Services AppHost
	public class AppHost : AppHostHttpListenerBase
	{
		readonly CompositionContainer _compositionContainer;
		
		public AppHost(string extensionsPath)
			: base("OpenThesis Server", typeof(AppHost).Assembly)
		{
			var catalog = new AggregateCatalog();
			catalog.Catalogs.Add(new DirectoryCatalog(extensionsPath));
			_compositionContainer = new CompositionContainer(catalog);
			_compositionContainer.ComposeParts(this);
		}

		[Import]
		ITradesRepository _radesRepository;

		// Do any bootstrapping here
		public override void Configure(Funq.Container container)
		{

			Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] { new CredentialsAuthProvider() }));

			container.Register<ICacheClient>(new MemoryCacheClient());
			container.Register<ITradesRepository>(_radesRepository);
		}

		public override void Start(string urlBase)
		{
			base.Start(urlBase);
		}

		public override void Stop()
		{
			base.Stop();
		}
	}
}
